import QtQuick 2.8
import QtQuick.Layouts 1.2
import QtQuick.Controls 2.14

Page {
    id: pageTaskC
    title: qsTr("选择场次")
    visible: true
 
    property StackView stack: null
    property var posId
 
    Keys.enabled: true
    focus: true
    Keys.onPressed: {

        if(event.key == Qt.Key_2) {
            let index = listView.currentIndex //https://doc.qt.io/qt-5/qml-qtquick-listview.html#currentIndex-prop
            if (index > -1)
                listView.currentIndex = index - 1
            else
                listView.currentIndex = 0
        }
        if(event.key == Qt.Key_8) {
            let index = listView.currentIndex
            if (index < listView.count)
                listView.currentIndex = index + 1
            else
                listView.currentIndex = 0
        }
        if(event.key == Qt.Key_Escape){
            stack.custPop()
        }
        if(event.key == Qt.Key_F1){
            var item = listView.model.get(listView.currentIndex);
            if(item == null){
                return rootWindow.popupTips('请选择批次')
            }
            // pageTaskCounter.visible = true;
            // pageTaskCounter.stack = stack;//设置窗口栈
            // stack.push(pageTaskCounter);//入栈
            rootWindow.batchName = item.batch;
            rootWindow.batchCount = item.count;
            // stack.custPush('PageTaskCounter.qml', {})
            stack.custPush('PageTaskD.qml', {})
        }
        if(event.key == Qt.Key_3){
            fetchData()
        }
        if(event.key == Qt.Key_1){
            var item = listView.model.get(listView.currentIndex);
            if(item == null){
                return rootWindow.popupTips('请选择批次')
            }
            rootWindow.batchName = item.batch;
            rootWindow.batchCount = item.count;
            stack.custPush('PageSubmits.qml', {
                
            })
        }
        event.accepted = true
    }
    
    function fetchData(){
        // console.log('posId', posId);

        let res = gHttp.fetchBatchs(posId);

        var json = JSON.parse(res);        
        if(json.code < 0){
            rootWindow.popupTips(json.message, 1000);
            return;
        }
        
        let list = json.data;
        listView.model.clear();
        for(var i in list){
            list[i].colorCode = "green";
            listModel.append(list[i])
        }
    }
    header: ToolBar {
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#3f51b5"
        }
        RowLayout {
            anchors.fill: parent
            ToolButton {
                text: qsTr("‹返回[电源键]")
                onClicked: stack.custPop()
            }
            Label {
                text: "请选择场次"
                font.pointSize: 16
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }
            // ToolButton {
            //     text: qsTr("⋮")
            //     onClicked: menu.open()
            // }
        }
    }
    footer: Rectangle{
        width: parent.width
        height: 40
        color: '#3f51b5'
        
        Text {
            anchors.centerIn: parent
            font.pointSize: 16
            color: '#ffffff'
            text: "上翻[2] 下翻[8] 确认[计数键] 查询[3] 查询已计数[1] "
        }
    }
    ListModel { //链表模型
        id: listModel
        // ListElement { //链表元素
        //     name: "衬衣"
        //     colorCode: "green"
        // }
    }
    ListView { //https://doc.qt.io/qt-5/qml-qtquick-listview.html //中文手册：https://www.bookstack.cn/read/QmlBook-In-Chinese/model-view-delegate-dynamic_views.md
        id: listView
        clip: true
        width: parent.width;
        height: parent.height;
        spacing :0
        highlightFollowsCurrentItem: true 
        focus: true
        ScrollBar.vertical: ScrollBar {
            policy: ScrollBar.AlwaysOn      
        }
        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        delegate: Rectangle {
            color: 'transparent'
            width: listView.width
            height: 40
            Row {
                anchors.fill: parent
                spacing: 10
                anchors.leftMargin: 10
                Rectangle {
                    width: 30
                    height: 30
                    color: colorCode
                    radius: width/2;
                    anchors.verticalCenter: parent.verticalCenter
                }
                Text {
                    text: '场次：' + batch + ' ---- 考场总数：' + count
                    anchors.verticalCenter: parent.verticalCenter
                    font.bold: true
                    font.pointSize: 23
                    color: "gray"
                }
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onClicked: {
                    listView.currentIndex = index;
                    //myListModel.changeAll(index)
                }
            }
        }
        model: listModel //https://www.bookstack.cn/read/QmlBook-In-Chinese/model-view-delegate-basic_model.md
    }

    Component.onCompleted: { 
        
        // print("PageTaskB.onCompleted() called")
        
    }
    
    function initialAction(){
        
        fetchData()
    }
    
    // PageTaskCounter {
    //     id: pageTaskCounter
    //     visible: false
    // }
}